package com.da.alipay.controller;

import com.alipay.api.AlipayApiException;
import com.da.alipay.bean.AlipayBean;
import com.da.alipay.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 订单接口
 *
 * @author Louis
 * @date Dec 12, 2018
 */
@RestController()
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private PayService payService;

    /**
     * 阿里支付
     *
     * @param outTradeNo
     * @param subject
     * @param totalAmount
     * @param body
     * @throws AlipayApiException
     */
    @PostMapping(value = "/alipay")
    public String alipay(String outTradeNo, String subject, String totalAmount, String body) throws AlipayApiException {
        AlipayBean alipayBean = new AlipayBean();
        alipayBean.setOut_trade_no(outTradeNo);
        alipayBean.setSubject(subject);
        alipayBean.setTotal_amount(totalAmount);
        alipayBean.setBody(body);
        return payService.aliPay(alipayBean);
    }

    /**
     * http://127.0.0.1:8080/order/queryOrder?outTradeNo=dzcp100010005
     */
    @GetMapping(value = "/queryOrder")
    public String queryOrder(@RequestParam("outTradeNo") String outTradeNo) throws AlipayApiException {
        return payService.payQuery(outTradeNo);
    }

    /**
     * http://127.0.0.1:8080/order/test
     */
    @GetMapping(value = "/test")
    public String alipay() throws AlipayApiException {
        AlipayBean alipayBean = new AlipayBean();
        alipayBean.setOut_trade_no("1611642435235");
        alipayBean.setSubject("测试订单");
        alipayBean.setTotal_amount("0.01");
        alipayBean.setBody(null);
        return payService.aliPay(alipayBean);
    }
}
